//
//  GXCommonIntroduceController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/13.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXCommonIntroduceController.h"

@interface GXCommonIntroduceController ()<UITextViewDelegate>
@property (nonatomic,strong) UILabel * placeHolderLabel;
@property (nonatomic,strong) UITextView * myTextView;
@property (nonatomic,strong) UILabel * limitLabel;
@end

@implementation GXCommonIntroduceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleName;
    self.view.backgroundColor = kGlobalbgColor;
    [self setUpNavigationRightBarButtonItem];
    [self creatTextView];
}
-(void)setUpNavigationRightBarButtonItem
{
    UIButton *rightBtn = [UIButton setButtonTitleNormal:@"清空" buttonTintColor:[UIColor whiteColor]];
    [rightBtn addTarget:self action:@selector(enterRightButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem=rightBarBtn;
}
-(void)enterRightButton:(UIButton *)btn
{
    _myTextView.text = @"";
    _placeHolderLabel.hidden = NO;
    _limitLabel.text = @"0/200";
}
- (UILabel *)placeHolderLabel
{
    if (_placeHolderLabel == nil) {
        UILabel *placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 260, 20)];
        placeHolderLabel.text =self.placeHolderStr;
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        placeHolderLabel.font = [UIFont systemFontOfSize:13];
        [placeHolderLabel sizeToFit];
        _placeHolderLabel = placeHolderLabel;
    }
    return _placeHolderLabel;
}
- (void)creatTextView
{
    UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 15, kUIScreenWidth-20, 200)];
    textView.font = [UIFont systemFontOfSize:15];
    _myTextView = textView;
    textView.delegate = self;
    textView.backgroundColor = [UIColor whiteColor];
    textView.textColor=[UIColor lightGrayColor];
    [self.view addSubview:textView];
    [textView addSubview:self.placeHolderLabel];
    textView.layer.masksToBounds = YES;
    [textView.layer setCornerRadius:5];
    _myTextView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    UILabel * limitLabel = [[UILabel alloc]initWithFrame:CGRectMake(kUIScreenWidth-100-10, textView.height+20, 100, 15)];
    limitLabel.textColor = [UIColor darkGrayColor];
    limitLabel.font = [UIFont systemFontOfSize:16];
    limitLabel.textAlignment = NSTextAlignmentRight;
    _limitLabel = limitLabel;
    limitLabel.text = @"0/200";
    [self.view addSubview:limitLabel];
    
    
    UIButton * saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    saveBtn.frame = CGRectMake(0, kUIScreenHeight-40-Height_NavBar, kUIScreenWidth,40);
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.backgroundColor = RGBColor(114, 216, 207);
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [saveBtn addTarget:self action:@selector(clickSaveBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveBtn];
}
- (void)clickSaveBtn:(UIButton *)saveBtn
{
    if ([NSString isBlankString:self.myTextView.text]) {
        [SVProgressHUD showErrorWithStatus:@"输入菜品描述为空"];
        return;
    }
    WeakSelf(weakSelf);
    if (weakSelf.describleBlock) {
        weakSelf.describleBlock(self.myTextView.text);
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.myTextView resignFirstResponder];
}
#pragma UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    _placeHolderLabel.hidden = YES;
    [self.myTextView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1) {
        _placeHolderLabel.hidden = NO;
    }else
    {
        _placeHolderLabel.hidden = YES;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //判断加上输入的字符，是否超过界限
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 200)
    {
        NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:200];
        if (rangeIndex.length == 1)
        {
            
            textView.text = [str substringToIndex:200];
            //这里重新统计下字数，字数超限，我发现就不走textViewDidChange方法了，你若不统计字数，忽略这行
            self.limitLabel.text = [NSString stringWithFormat:@"%lu/%d", (unsigned long)textView.text.length, 200];
        }else{
            NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 200)];
            textView.text = [str substringWithRange:rangeRange];
        }
        return NO;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 200)
    {
        textView.text = [textView.text substringToIndex:200];
    }
    //记录输入的字数，你若不统计字数，忽略这行
    self.limitLabel.text = [NSString stringWithFormat:@"%lu/%d", (unsigned long)[textView.text length],200];
}



@end
